package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.convert.NumberChineseFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.poi.PoiTlUtils;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.entity.hy.BusHyorderInovice;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceDetailsService;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysArea;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysAreaService;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 订单明细控制器
 *
 * @author flyme
 * @date 2020-08-19
 */
@RestController
@RequestMapping("/bus/")
public class HyExportController extends BaseController<BusOrderDetailsService, BusOrderDetails> {

    @Autowired
    private HyOrderDetailService orderDetailService;

    @Autowired
    private HyOrderService orderService;

    @Autowired
    private BusCustomService customService;
    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private SysAreaService areaService;

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;

    @Autowired
    private BusHyorderInoviceDetailsService hyorderInoviceDetailsService;

    @Autowired
    private BaseUserService userService;

    @Autowired
    private BusHyorderInoviceService hyorderInoviceService;

    @PostMapping(value = "export/gxht")
    public void exportGxht(@RequestParam(required = false) Map params, HttpServletResponse response) {
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        EntityMap map = new EntityMap(params);
        String orderId = map.get("orderId");
        Long proId = map.getLong("proId");
        Long cityId = map.getLong("cityId");
        Long areaId = map.getLong("areaId");
        String payTypeValue = map.get("payTypeValue", "1");
        String dayNum = map.get("dayNum", "/");
        String transportTypeValue = map.get("transportTypeValue", "1");
        String otherTransportTypeValue = map.get("otherTransportTypeValue", "/");
        HyOrder order = orderService.getById(orderId);
        Long companyId = order.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        List<EntityMap> orderDetailList = orderDetailService.selectMapByOrderId(orderId);
        Long customId = order.getCustomId();
        BusCustom custom = customService.getById(customId);
        BigDecimal totalAmount = order.getTotalSaleAmount();
        String amountStr = NumberChineseFormatter.format(totalAmount.doubleValue(), true, true);
        String customName = custom.getCustomName();
        Boolean isZxs = false;
        if (FlymeUtils.isNotEmpty(proId)) {
            SysArea area = areaService.getById(proId);
            String areaName = area.getAreaName();
            if (areaName.equals("重庆市") || areaName.equals("北京市") || areaName.equals("上海市") || areaName.equals("天津市")) {
                params.put("cityName", area.getAreaName().replace("市", ""));
                params.put("proName", "    /     ");
                isZxs = true;
            } else {
                params.put("proName", area.getAreaName().replace("省", ""));
            }
        }
        if (FlymeUtils.isNotEmpty(cityId)) {
            SysArea city = areaService.getById(cityId);
            if (isZxs) {
                params.put("areaName", city.getAreaName().replace("市", "").replace("区", ""));
            } else {
                params.put("cityName", city.getAreaName().replace("市", ""));
            }
        }
        if (FlymeUtils.isNotEmpty(areaId)) {
            SysArea area = areaService.getById(areaId);
            params.put("areaName", area.getAreaName().replace("区", "").replace("县", ""));
        } else {
            if (!isZxs) {
                params.put("areaName", "      /      ");
            }
        }
        String payType = "1.☐电汇       2.☑货到";
        if (payTypeValue.equals("1")) {
            payType = "1.☑电汇       2.☐货到";
            dayNum = "/";
        }
        String transportType = "1.☑公路       2.☐铁路       3.☐航空";
        if (transportTypeValue.equals("2")) {
            transportType = "1.☐公路       2.☑铁路       3.☐航空";
        }
        if (transportTypeValue.equals("3")) {
            transportType = "1.☐公路       2.☐铁路       3.☑航空";
        }
        if (transportTypeValue.equals("4")) {
            transportType = "1.☐公路       2.☐铁路       3.☐航空";
        }
        params.put("payType", payType);
        params.put("dayNum", dayNum);
        params.put("transportType", transportType);
        params.put("otherValue", otherTransportTypeValue);
        params.put("amountStr", amountStr);
        params.put("customName", custom.getCustomName());
        params.put("orderDetailList", orderDetailList);
        params.put("year", DateUtils.getYear());
        params.put("month", DateUtils.getMonth());
        params.put("day", DateUtils.getDay());
        Configure config = Configure.builder().bind("orderDetailList", policy).build();
        PoiTlUtils.write(response, "E:/word/gxht-" + companyId + ".docx", company.getCompanyName() + "购销合同(" + customName + ").doc", params, config);
    }

    @PostMapping(value = "export/gxht_cg")
    public void exportGxhtCg(@RequestParam(required = false) Map params, HttpServletResponse response) {
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        EntityMap map = new EntityMap(params);
        String orderId = map.get("orderId");
        String dayNum = map.get("dayNum", "/");
        HyOrder order = orderService.getById(orderId);
        Long companyId = order.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        List<EntityMap> orderDetailList = orderDetailService.selectMapByOrderId(orderId);
        Long customId = order.getCustomId();
        BusCustom custom = customService.getById(customId);
        BigDecimal totalAmount = order.getTotalSaleAmount();
        String amountStr = NumberChineseFormatter.format(totalAmount.doubleValue(), true, true);
        String customName = custom.getCustomName();
        params.put("dayNum", dayNum);
        params.put("amountStr", amountStr);
        params.put("customName", custom.getCustomName());
        params.put("orderDetailList", orderDetailList);
        params.put("year", DateUtils.getYear());
        params.put("month", DateUtils.getMonth());
        params.put("day", DateUtils.getDay());
        Configure config = Configure.builder().bind("orderDetailList", policy).build();
        PoiTlUtils.write(response, "E:/word/gxht-" + companyId + "-1.docx", company.getCompanyName() + "购销合同(" + customName + ").doc", params, config);
    }

    @PostMapping(value = "export/fwxy")
    public void exportFwxy(@RequestParam(required = false) Map params, HttpServletResponse response) {
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        EntityMap map = new EntityMap(params);
        Long inoviceId = MapUtil.getLong(params, "inoviceId");
        String dayNum = map.get("dayNum", "/");
        BusHyorderInovice hyorderInovice = hyorderInoviceService.getById(inoviceId);
        Long invoiceUnitId = hyorderInovice.getInvoiceUnitId();
        ApiAssert.isNotEmpty("开票单位不存在", invoiceUnitId);
        Long companyId = hyorderInovice.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        List<EntityMap> orderDetailList = hyorderInoviceDetailsService.selectByInvoiceId(inoviceId);
        BusInvoiceunit invoiceunit = invoiceunitService.getById(invoiceUnitId);
        BigDecimal totalSettleAmount = hyorderInovice.getInvoiceAmount();
        List<EntityMap> orderDetailList2 = hyorderInoviceDetailsService.selectByInvoiceId2(inoviceId);
        Date contractDate = invoiceunit.getContractDate();
        if (FlymeUtils.isNotEmpty(contractDate)) {
            params.put("year", contractDate.getYear() + 1900);
            params.put("month", contractDate.getMonth() + 1);
            params.put("day", contractDate.getDay());
        }
        Date settleBeginDate = hyorderInovice.getSettleBeginDate();
        Date settleEndDate = hyorderInovice.getSettleEndDate();
        if (FlymeUtils.isNotEmpty(settleBeginDate)) {
            params.put("y1", settleBeginDate.getYear() + 1900);
            params.put("m1", settleBeginDate.getMonth() + 1);
            params.put("d1", settleBeginDate.getDay());
        }
        if (FlymeUtils.isNotEmpty(settleEndDate)) {
            params.put("y2", settleEndDate.getYear() + 1900);
            params.put("m2", settleEndDate.getMonth() + 1);
            params.put("d2", settleEndDate.getDay());
        }
        String amountStr = NumberChineseFormatter.format(totalSettleAmount.doubleValue(), true, true);
        BigDecimal saleAmount = FlymeUtils.getBigDecimal(hyorderInovice.getSaleAmount(), "500");

        params.put("invoiceunitName", invoiceunit.getInvoiceunitName());
        params.put("companyName", company.getCompanyShortName());
        params.put("dayNum", dayNum);
        params.put("saleAmount", saleAmount.intValue());
        params.put("bankName", invoiceunit.getBankName());
        params.put("bankNo", invoiceunit.getBankNo());
        params.put("amountStr", amountStr);
        params.put("totalSettleAmount", totalSettleAmount);
        params.put("orderDetailList", orderDetailList);
        params.put("orderDetailList2", orderDetailList2);
        Configure config = Configure.builder().bind("orderDetailList", policy).bind("orderDetailList2", policy).build();
        PoiTlUtils.write(response, "E:/word/fwxy.docx", company.getCompanyName() + "服务协议.doc", params, config);
    }

    @PostMapping(value = "export/kpsq")
    public void exportKpsq(@RequestParam(required = false) Map params, HttpServletResponse response) {
        Long inoviceId = MapUtil.getLong(params, "inoviceId");
        EntityMap result = new EntityMap();
        BusHyorderInovice hyorderInovice = hyorderInoviceService.getById(inoviceId);
        Long userId = hyorderInovice.getUserId();
        BaseUser user = userService.getById(userId);
        if (FlymeUtils.isNotEmpty(user)) {
            result.put("nickName", user.getNickName());
        }
        Long companyId = hyorderInovice.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        result.put("companyName", company.getCompanyName());
        String unitName = "";
        String bankName = company.getBankName();
        String bankNo = company.getBankNo();
        String linkTel = company.getLinkTel();
        String address = company.getCompanyAddress();
        String businessLicNo = company.getBusinessLicNo();
        Long invoiceUnitId = hyorderInovice.getInvoiceUnitId();
        Integer invoiceType = hyorderInovice.getInvoiceType();
        BusInvoiceunit invoiceUnit = invoiceunitService.getById(invoiceUnitId);
        unitName = invoiceUnit.getInvoiceunitName();
        if (invoiceType.equals(2)) {
            result.put("invoiceType", "专票");
        } else {
            result.put("invoiceType", "普票");
        }
        String drugsName = hyorderInoviceDetailsService.getDrugsNames(inoviceId);
        result.put("unitName", unitName);
        result.put("drugsName", drugsName);
        result.put("bankName", bankName);
        result.put("bankNo", bankNo);
        result.put("linkTel", linkTel);
        result.put("address", address);
        result.put("invoiceAmount", hyorderInovice.getInvoiceAmount());
        result.put("title", hyorderInovice.getTitle());
        result.put("noTaxAmount", hyorderInovice.getNoTaxAmount());
        result.put("secondTaxAmount", hyorderInovice.getSecondTaxAmount());
        result.put("taxAmount", hyorderInovice.getTaxAmount());
        result.put("senondInvoiceAmount", hyorderInovice.getSenondInvoiceAmount());
        result.put("businessLicNo", businessLicNo);
        result.put("createTime", DateUtils.formatDate(hyorderInovice.getInvoiceDate()));
        PoiTlUtils.write(response, "E:/word/kpsq.docx", company.getCompanyName() + "-" + unitName + "-开票申请单.doc", result);
    }

}
